/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.lidejia.hivetotalsort.type;

import org.apache.hadoop.io.WritableComparator;

/**
 *
 * @author lidejia
 */
public class HiveTypeComparator extends WritableComparator {

    public HiveTypeComparator() {
        super(HiveType.class);
    }

    @Override
    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
        // skip length
        s1 += 4;
        s2 += 4;
        l1 -= 4;
        l2 -= 4;
        //
        byte orderReverse = b1[s1];
        s1 += 1;
        s2 += 1;
        l1 -= 1;
        l2 -= 1;
        //
        byte classType = b1[s1];
        s1 += 1;
        s2 += 1;
        l1 -= 1;
        l2 -= 1;
        //
        WritableComparator comparator = get(HiveType.SUPPORTED_CLASSES[classType]);
        int cmp = comparator.compare(b1, s1, l1, b2, s2, l2);
        if (orderReverse == 1) {
            cmp = -cmp;
        }
        return cmp;
    }
}
